
public class Table {
	
	public static final int WAIT_JOIN=1;//三缺一，等待牌友加入
	public static final int WAIT_READY=2;//等待开始
	public static final int AUCTION=3;//叫牌
	public static final int PLAY=4;//正在打牌
	public static final int END=5;//结束
	public static final int NEWLOOP=5;//新的一局
	
    int bid=0;//底分
    int curLoop=0;//第几局
    int landlord=-1;//谁是地主
 	int playerNum=0;//
 	int auctionTime=0;//叫几分
 	int passTime=0;
    int currentPlayer;
    int scoreMultiple=1;//翻几倍
    int gameState=WAIT_JOIN;//游戏状态，初始状态为等待加入
	int[] lastCards=new int[3];//底牌
	int[] bigCards;//当前最大的牌
 	boolean[] seat={false,false,false};
 	
    Player[] players=new Player[3];

    public void setLastCards(int[] cards){
    	lastCards=cards;
    	}
    
    public void setGameState(int gameState){
    	this.gameState=gameState;
    	}
    public int getGameState(){
    	return gameState;
    	}
    public String addPlayer(Player m)	{
    	String msg="cmd="+Connection.CMD_JOIN;
    	if(playerNum<3) {
    		for(int i=0;i<3;i++){
    			if(!seat[i]) {
    				players[i]=m;
    				m.seatNum=i;
    				seat[i]=true;
    				msg+="&ID="+m.id
    					+"&name="+m.name
    					+"&score="+m.score
    					+"&seat="+m.seatNum;
    				break;
    			}
    		}
    		playerNum++;
    		if(playerNum==3&&gameState==WAIT_JOIN) setGameState(WAIT_READY);
    		msg+="&gameState="+gameState;
    		}
    	return msg;	
    }
    
    public void removePlayer(Player player){
    	players[player.seatNum]=null;
    	seat[player.seatNum]=false;
    	playerNum--;
    	setGameState(WAIT_JOIN);
    	}
    
    public String addPlayer(Player player, int seatNum){
    	String msg="cmd="+Connection.CMD_JOIN;
    	if(seatNum>=0&&seatNum<3){
    		if(!seat[seatNum]) {
    			players[seatNum]=player;
    			player.seatNum=seatNum;
    			seat[seatNum]=true;
    			playerNum++;
    			if(playerNum==3&&gameState==WAIT_JOIN) setGameState(WAIT_READY);
    			msg+="&ID="+player.id
    				+"&name="+player.name
    				+"&score="+player.score
    				+"&seat="+player.seatNum;
    		}
    		else{
    			removePlayer(players[seatNum]);
    			addPlayer(player,seatNum);
    		}
    	msg+="&gameState="+gameState;	
    	}
    	return msg;
    	}
    
    public String tableInfo(){
    	String str="playerNum="+playerNum;
    	for(int i=0;i<3;i++){
    		if(seat[i]) str=str+"&seat"+i+"=1"
    					+"&player"+i+"="+players[i].name
    					+"&id"+i+"="+players[i].id
    					+"&seatNum"+i+"="+players[i].seatNum
    					+"&score"+i+"="+players[i].score
    					+"&getGameState"+i+"="+players[i].state;
    		else str=str+"&seat"+i+"=0";			
    	}
    	return 	str;
    	}
        
    public void showMsg(String msg){
    	for(int i=0;i<3;i++){
    		if(seat[i]) players[i].conn.sendMsg(msg);
    	}
    }
    
    public boolean isReady(){
    	if(playerNum==3){
    		if(players[0].state== Player.READY&&players[1].state== Player.READY&&players[2].state== Player.READY) return true;
    		else return false;
    	}
    	else return false;
    }
    
    public int [] getBigCards(){
		int [] newCards;
		if(bigCards!=null){
			newCards=new int[bigCards.length];
			System.arraycopy(bigCards,0,newCards,0,bigCards.length);
			java.util.Arrays.sort(newCards);
			return newCards;
		}
		else return null;
	}
	
	public void newGame(){
		bid=0;
		curLoop=0;
		landlord=0;
		auctionTime=0;
		passTime=0;
		scoreMultiple=1;
		if(playerNum==3)gameState=WAIT_READY;
		else gameState=WAIT_JOIN;
		lastCards=null;
		bigCards=null;
		for(int i=0;i<3;i++) 
			if(seat[i])players[i].newGame();	
	}			
}